package com.hua.api.core.framework.pay;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.hua.api.core.framework.config.SysConstant;
import com.hua.api.core.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.FileCopyUtils;

import java.io.IOException;
import java.io.InputStream;

/**
 * 接入文档参考 :  https://opendocs.alipay.com/common/02kdnc
 * https://opendocs.alipay.com/open/8ad49e4a_alipay.trade.precreate?scene=2ae8516856f24a5592194d237f3f235d&pathHash=d18bff53
 *
 * */

@Slf4j
@Configuration
@EnableConfigurationProperties()
public class AlipayConfiguration {

    @Autowired
    private SysConstant sysConstant;

    @Bean
    public AlipayClient alipayClient() throws AlipayApiException {
        log.info("阿里支付宝支付服务初始化 AlipayClient");
        log.info("Config.appId={}", sysConstant.getAppId());
        log.info("Config.privateKey={}", sysConstant.getPrivateKey());
        log.info("Config.serverUrl={}", sysConstant.getServerUrl());
        log.info("Config.publicKey={}", sysConstant.getPublicKey());
        log.info("Config.charset={}", sysConstant.getCharset());
        log.info("Config.signType={}", sysConstant.getSignType());
        log.info("Config.rootCertPath={}", sysConstant.getRootCertPath());
        log.info("Config.appCertPath={}", sysConstant.getAppCertPath());
        log.info("Config.certPath={}", sysConstant.getAlipayCertPath());
        log.info("Config.format={}", sysConstant.getFormat());
        log.info("Config.alipayNotifyUrl={}",sysConstant.getAlipayNotifyUrl());


        AlipayConfig config = new AlipayConfig();
        config.setServerUrl(sysConstant.getServerUrl());
        config.setAppId(sysConstant.getAppId());
        config.setPrivateKey(sysConstant.getPrivateKey());
        config.setFormat(sysConstant.getFormat());
        config.setAlipayPublicKey(sysConstant.getPublicKey());
        config.setCharset(sysConstant.getCharset());
        config.setSignType(sysConstant.getSignType());

        if(StringUtil.equals("dev",sysConstant.active)){
            config.setAppCertContent(getCertContentByPath(sysConstant.getAppCertPath()));
            config.setRootCertContent(getCertContentByPath(sysConstant.getRootCertPath()));
            config.setAlipayPublicCertContent(getCertContentByPath(sysConstant.getAlipayCertPath()));
        }else{
            config.setAppCertPath(sysConstant.getAppCertPath());
            config.setAlipayPublicCertPath(sysConstant.getAlipayCertPath());
            config.setRootCertPath(sysConstant.getRootCertPath());
        }

        return new DefaultAlipayClient(config);
    }

    public String getCertContentByPath(String name){
        InputStream inputStream = null;
        String content = null;
        try{
            inputStream = this.getClass().getClassLoader().getResourceAsStream(name);
            content = new String(FileCopyUtils.copyToByteArray(inputStream));
            log.info("content======{}",content);
        }catch (Exception e){
           log.error("getCertContentByPath error = {}",e.getMessage());
            // e.printStackTrace();
        }
        return content;
    }


}
